package study

import (
	"context"
	"encoding/json"
	"ez/apps/study/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type WordTestPaper struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64 `json:"id" bson:"id,omitempty"`

	Name string `json:"name" bson:"name"` //标题

	Ids []int64 `json:"ids" bson:"ids"` //所选的题目ID

	CreateAt time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *WordTestPaper) DocName() string { return "WordTestPaper" }

func (this *WordTestPaper) GetId() int64 { return this.Id }

func (this *WordTestPaper) SetId(id int64) { this.Id = id }

func (this *WordTestPaper) Create() error {
	return this.GetFactory().Create(this)
}

func (this *WordTestPaper) Replace() error {
	return this.GetFactory().Replace(this)
}

func (this *WordTestPaper) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *WordTestPaper) Delete() error {
	return this.GetFactory().Delete(this)
}

func (this *WordTestPaper) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *WordTestPaper) Destroy() error {
	return this.GetFactory().Destroy(this)
}

func (this *WordTestPaper) ToString() string {
	return string(this.ToBytes())
}

func (this *WordTestPaper) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *WordTestPaper) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *WordTestPaper) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}

func (this *WordTestPaper) ClearRelationsBeforeSave() mgo.Doc {

	return this
}

func neverUsed_WordTestPaper() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type WordTestPaperAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *WordTestPaper
	Session *ez.Session
}

func NewWordTestPaperAccessControl(model *WordTestPaper, action string, session *ez.Session) *WordTestPaperAccessControl {
	ctrl := &WordTestPaperAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.WordTestPaperAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
